package cn.dengta.webapp.trade.dto;

import java.util.*;

import cn.dengta.common.util.Common;
import cn.dengta.context.model.Member;
import cn.dengta.context.rpc.CachedConfig;
import cn.dengta.context.rpc.CachedConfigManager;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 充值风控，大额实名制配置
 *
 * @author lizhichao
 * @since 2023-06-12
 */
@Setter
@Getter
public class PayRiskRealNameConfig {

	/** 强制实名制 */
	private Boolean realName;
	/** 确认充值开关 */
	private Boolean confirmRecharge;
	/** 触发金额 */
	private Integer triggerAmount;

	/** 强制弹窗开关 */
	private Boolean forcePopup;
	/** 触发金额 */
	private Integer forceTriggerAmount;
	/** 触发提示 */
	private String forceTriggerTips;

	/** 强制弹窗开关 */
	private Boolean forceAQ;
	/** 强制答题金额 */
	private Integer forceAQAmount;
	/** 是否冻结开关 */
	private Boolean forceAQFreeze;
	/** 答错次数 */
	private Integer wrongAnswerNum;
	/** 冻结时长（分钟） */
	private Integer freezeDuration;
	/** 手机验证 */
	private Boolean phoneVerify;
	/** 强制答题黑名单，多个用逗号隔开 */
	private String forceAQBlacklist;

	private transient Set<Long> forceAQBlacklistSet;
	/** 停止推荐开关 */
	private Boolean stopRec;
	/** 停止推荐金额 */
	private Integer stopRecAmount;
	/** 风控提示开关 */
	private Boolean riskTipsEnable;

	public static CachedConfig<PayRiskRealNameConfig> cache() {
		return InstanceHolder.INSTANCE;
	}

	public static PayRiskRealNameConfig getDefault() {
		final PayRiskRealNameConfig cfg = new PayRiskRealNameConfig();
		cfg.triggerAmount = 200000;
		cfg.confirmRecharge = true;
		cfg.realName = true;
		cfg.forcePopup = false;
		cfg.forceTriggerAmount = 300000;
		cfg.forceAQAmount = 500000;
		cfg.forceAQ = false;
		cfg.forceAQFreeze = false;
		cfg.wrongAnswerNum = 3;
		cfg.freezeDuration = 10;
		cfg.phoneVerify = false;
		cfg.forceAQBlacklist = null;
		cfg.forceAQBlacklistSet = Collections.emptySet();
		cfg.stopRec = false;
		cfg.stopRecAmount = 50000;
		cfg.riskTipsEnable = true;
		return cfg;
	}

	private static final class InstanceHolder {

		static final CachedConfig<PayRiskRealNameConfig> INSTANCE = CachedConfigManager.create("pay_risk_real_name_config", PayRiskRealNameConfig.class, PayRiskRealNameConfig::getDefault);

	}

	public boolean matchForceBlacklist(Long userId) {
		if (StringUtil.isEmpty(forceAQBlacklist)) {
			return false;
		}

		if (forceAQBlacklistSet == null) {
			forceAQBlacklistSet = new HashSet<>(Common.split(forceAQBlacklist, Common.CHAR_SEP, Member::decode, true));
		}

		if (X.isValid(forceAQBlacklistSet)) {
			return forceAQBlacklistSet.contains(userId);
		}
		return false;
	}

	public Boolean getForcePopup() {
		return forcePopup != null && forcePopup;
	}

	public Integer getForceTriggerAmount() {
		return forceTriggerAmount == null ? 300000 : forceTriggerAmount;
	}

	public String getForceTriggerTips() {
		return forceTriggerTips == null ? "" : forceTriggerTips;
	}

	public Boolean getForceAQ() {
		return forceAQ != null && forceAQ;
	}

	public Boolean getForceAQFreeze() {
		return forceAQFreeze != null && forceAQFreeze;
	}

	public Integer getWrongAnswerNum() {
		return wrongAnswerNum == null ? 3 : wrongAnswerNum;
	}

	public Integer getFreezeDuration() {
		return freezeDuration == null ? 10 : freezeDuration;
	}

	public Integer getForceAQAmount() {
		return forceAQAmount == null ? 500000 : forceAQAmount;
	}

	public Boolean getPhoneVerify() {
		return phoneVerify != null && phoneVerify;
	}

	public Boolean getStopRec() {
		return stopRec != null && stopRec;
	}

	public Integer getStopRecAmount() {
		return stopRecAmount == null ? 50000 : stopRecAmount;
	}

	public Boolean getRiskTipsEnable() {
		return riskTipsEnable != null && riskTipsEnable;
	}

}
